const express = require('express')
const bodyParser = require('body-parser')
const multer = require('multer')
const request = require('request-promise')
const app = express()
const api_key = 'AnMcbe_Gi9msizyM9c4tMB8iO3-RqAsP';
const api_secret = 'hp4XU50OsNFl26kYbOpKJGP67BbjqFpK';
const faceset_token = 'be892562c0aab896bfc02d8fee6cc544';

app.use(bodyParser.json()); // for parsing application/json
app.use(bodyParser.urlencoded({ extended: true })); // for parsing application/x-www-form-urlencoded

app.get('/', function(req, res) {
    res.send('Hello World!')
})
app.use('/static', express.static('static'))

app.post('/upload_face', function(req, res) {
    console.log("in upload_face");
    console.log("body is " + req.body.face_image);
    console.log("api_key is " + api_key);
    var face_token = null;
    request({
            method: "POST",
            url: "https://api-cn.faceplusplus.com/facepp/v3/detect",
            form: {
                api_key: api_key,
                api_secret: api_secret,
                image_base64: req.body.face_image
            },
            json: true
        })
        .then(function(response) {
            console.log("in detect");
            console.dir(response);
            face_token = response.faces[0].face_token;
            request({
                    method: "POST",
                    url: "https://api-cn.faceplusplus.com/facepp/v3/faceset/addface",
                    form: {
                        api_key: api_key,
                        api_secret: api_secret,
                        faceset_token: faceset_token,
                        face_tokens: face_token
                    },
                    json: true
                }).then(function(response) {
                    console.log("in addface");
                    console.dir(response);
                    request({
                            method: "POST",
                            url: 'https://api-cn.faceplusplus.com/facepp/v3/face/setuserid',
                            form: {
                                api_key: api_key,
                                api_secret: api_secret,
                                face_token: face_token,
                                user_id: req.body.face_name
                            },
                            json: true
                        }).then(function(response) {
                            console.log("in setuserid");
                            console.dir(response);
                        })
                        .catch(function(err) {
                            console.log("setuserid err is " + err.toString());
                        })
                })
                .catch(function(err) {
                    console.log("addface err is " + err.toString());
                })
        })
        .catch(function(err) {
            console.log("detect err is " + err.toString());
        })


    res.send({
        success: true
    });
})


app.post('/search', function(req, res) {
    request({
            method: "POST",
            url: 'https://api-cn.faceplusplus.com/facepp/v3/search',
            form: {
                api_key: api_key,
                api_secret: api_secret,
                faceset_token: faceset_token,
                image_base64: req.body.face_image
            },
            json: true
        }).then(function(response) {
            console.log("in search");
            console.dir(response);
            res.send({
                faces: response.results
            });
        })
        .catch(function(err) {
            console.log("setuserid err is " + err.toString());
        })
})

app.use(function(req, res, next) {
    res.header("Access-Control-Allow-Origin", "*");
    res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
    next();
});

app.listen(3000, function() {
    console.log('Example app listening on port 3000!')
})